﻿/* *
 * =============================================================================
 * File Name:               LightEmittingDiode.cs
 * Programmer:              Ken Watts
 * Creation Date:           8/3/2008
 * =============================================================================
 * Description:             Class that represents a light emitting diode.
 * =============================================================================
 * Modification Date:       8/3/2008 - Initial Revision
 * =============================================================================
 * */

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DigitalLogicSimulator
{
    public class LightEmittingDiode:Device
    {
        // Constants
        private const int k_int_num_inputs = 1;
        private const int k_int_input_x_offset = 2;
        private const int k_int_input_y_offset = 1;
        private const int k_int_first_input = 0;
        private const double k_initial_top = 92;
        private const double k_initial_left = 75;
        //private const double k_rotational_angle = -720;
        private const double k_radius = 10;
        // Center of LED
        private const double k_p1_x = 20;
        private const double k_p1_y = 10;
        // Inputs and outputs
        private const double k_in1_p1_x = 0;
        private const double k_in1_p1_y = 10;
        private const double k_in1_p2_x = 10;
        private const double k_in1_p2_y = 10;

        // Input state
        bool inputState = false;

        // Color value for "on"
        Color onColor, offColor;
        Brush onBrush, offBrush;

        public LightEmittingDiode()
        {
            // Create an array for the input devices
            inputDevices = new Device[k_int_num_inputs];
            // Setup colors
            backgroundColor = Color.FromArgb(0xaa, 0x80, 0x00, 0x00);
            onColor = Color.FromArgb(0xaa, 0xff, 0x00, 0x00);
            offColor = backgroundColor;
            onBrush = new SolidColorBrush(onColor);
            offBrush = new SolidColorBrush(offColor);
            backgroundBrush = new SolidColorBrush(backgroundColor);
        }

        protected override void ReplaceInstance(Panel parentPanel)
        {
            parentPanel.Children.Add(new LightEmittingDiode());
        }

        protected override void Draw()
        {
            pathGeometry = new PathGeometry();
            pathGeometry.FillRule = FillRule.Nonzero;
            EllipseGeometry eg = new EllipseGeometry();
            eg.Center = new Point(k_p1_x, k_p1_y);
            eg.RadiusX = k_radius;
            eg.RadiusY = k_radius;

            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = new Point(k_in1_p1_x, k_in1_p1_y);
            pathFigure.IsClosed = false;
            pathGeometry.Figures = new PathFigureCollection();
            pathGeometry.Figures.Add(pathFigure);
            pathFigure.Segments = new PathSegmentCollection();
            LineSegment ls = new LineSegment();
            ls.Point = new Point(k_in1_p2_x, k_in1_p2_y);
            pathFigure.Segments.Add(ls);

            // Setup the path
            path = new Path();
            path.Stretch = Stretch.Fill;
            path.StrokeLineJoin = PenLineJoin.Round;
            path.Stroke = new SolidColorBrush(strokeColor);
            path.Fill = new SolidColorBrush(backgroundColor);
            path.StrokeThickness = k_dbl_stroke_thickness;
            pathGroup = new GeometryGroup();
            pathGroup.Children.Add(pathGeometry);
            pathGroup.Children.Add(eg);
            path.Data = pathGroup;
            path.MouseLeftButtonDown += new MouseButtonEventHandler(CustomMouseLeftButtonDown);
            path.MouseLeftButtonUp += new MouseButtonEventHandler(CustomMouseLeftButtonUp);
            path.MouseMove += new MouseEventHandler(CustomMouseMove);
            path.MouseEnter += new MouseEventHandler(CustomMouseEnter);
            path.MouseLeave += new MouseEventHandler(CustomMouseLeave);

            Children.Add(path);
            
            // Set the initial position
            path.SetValue(Canvas.LeftProperty, k_initial_left);
            path.SetValue(Canvas.TopProperty, k_initial_top);
            canvLeft = k_initial_left;
            canvTop = k_initial_top;
        }

        protected override int Connect(Device logicDevice, Point clickedPoint)
        {
            if (inputDevices[k_int_first_input] == null)
            {
                inputDevices[k_int_first_input] = logicDevice;
                return k_int_first_input;
            }
            // Can't connect...
            return k_int_not_connected;
        }

        protected override Point GetInputLocation(int inputNumber)
        {
            Point p = new Point();
            // Figure out the correct point information
            if (inputNumber == k_int_first_input)
            {
                p.X = canvLeft + k_in1_p1_x + k_int_input_x_offset;
                p.Y = canvTop + k_in1_p1_y + k_int_input_y_offset;
            }
            return p;
        }

        // Sets the state of the specified input to the given value
        public override void SetInputState(int inputNumber, bool state)
        {
            if (inputNumber == k_int_first_input)
                // Set the first input state
                inputState = state;
            // Set the background to the appropriate color based on the state
            backgroundBrush=(inputState) ? onBrush : offBrush;
            path.Fill = backgroundBrush;
        }
    }
}
